import request from '@/utils/request';
import {download, toFormData, checkDownloadRes} from '@/utils/common';

/**
 * 分页查询报表类型管理
 */
export async function pageTypes(params) {
  const res = await request.get('/admin/jimu/report/type/page/list', {params});
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部报表类型管理
 */
export async function listTypes(params) {
  const res = await request.get('/admin/jimu/report/type/list', {params});
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询报表类型管理
 */
export async function getType(id) {
  const res = await request.get('/admin/jimu/report/type/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加报表类型管理
 */
export async function addType(data) {
  const res = await request.post('/admin/jimu/report/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改报表类型管理
 */
export async function updateType(data) {
  const res = await request.put('/admin/jimu/report/type', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除报表类型管理
 */
export async function removeType(id) {
  const res = await request.delete('/admin/jimu/report/type/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除报表类型管理
 */
export async function removeTypes(ids) {
  const res = await request.delete('/admin/jimu/report/type/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出报表类型管理
 */
export async function exportTypes(params) {
  const res = await request({
    url: '/admin/jimu/report/type/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `type_${Date.now()}.xlsx`);
}
